#2009 vs 2013

part07_age<- read.csv("part07_age.csv")
part09_age<- read.csv("part09_age.csv")
part11_age<- read.csv("part11_age.csv")
part13_age<- read.csv("part13_age.csv")
times100<- function(df){
  df$Mean<- df$Mean*100
  df$ci <- df$ci*100
  return(df)
}
part07_age<- times100(part07_age)
part09_age<- times100(part09_age)
part11_age<- times100(part11_age)
part13_age<- times100(part13_age)


df0913<- bind_rows(part09_age, part13_age)
gg0913_2<- ggplot(df0913, aes(x=age, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point()+ ggtitle("2009 to 2013")+xlab("Age")+ylab("Rate of participation")

df0913<- bind_cols(part09_age, part13_age)
df0913$diff<- df0913$Mean...7- df0913$Mean...2
df0913$diffci<- 1.96*100*sqrt( ((df0913$Mean...7/100)*(1-df0913$Mean...7/100)/df0913$Freq...8) + 
                             ((df0913$Mean...2/100)*(1-df0913$Mean...2/100)/df0913$Freq...3))
gg0913_1<- ggplot(df0913, aes(x=age...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Age")+ylab("Increase in participation")+ggtitle(" ")



#2007 vs 2011
df0711<- bind_rows(part07_age, part11_age)
gg0711_2<- ggplot(df0711, aes(x=age, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point()+ ggtitle("2007 to 2011")+xlab("Age")+ylab("Rate of participation")
df0711<- bind_cols(part07_age, part11_age)
df0711$diff<- df0711$Mean...7- df0711$Mean...2
df0711$diffci<- 1.96*100*sqrt( ((df0711$Mean...7/100)*(1-df0711$Mean...7/100)/df0711$Freq...8) + 
                             ((df0711$Mean...2/100)*(1-df0711$Mean...2/100)/df0711$Freq...3))
gg0711_1<- ggplot(df0711, aes(x=age...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Age")+ylab("Increase in participation")+ggtitle(" ")


library(cowplot)
prow_l<-plot_grid(gg0913_2,gg0913_1,gg0711_2,gg0711_1, ncol=2)
prow_l

